package cc.magicjson.jpa.datasouce.config;

import com.zaxxer.hikari.HikariDataSource;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef = "entityManagerFactoryPrimary",
    transactionManagerRef = "transactionManagerPrimary",
    basePackages = {"cc.magicjson.jpa.datasouce"}
)
public class DataSourceConfig {


    private final DataSourceProperties h2Properties;

    private final DataSourceProperties hsqlProperties;

    public DataSourceConfig(DataSourceProperties h2Properties, DataSourceProperties hsqlProperties) {
        this.h2Properties = h2Properties;
        this.hsqlProperties = hsqlProperties;
    }

    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(DataSource primaryDataSource) {
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setDataSource(primaryDataSource);
        emf.setPackagesToScan("cc.magicjson.jpa.datasouce");
        emf.setJpaPropertyMap(getVendorProperties(primaryDataSource));
        emf.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        return emf;
    }

    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }

    @Bean(name = "primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.h2")
    public DataSource primaryDataSource() {
        DataSourceBuilder<HikariDataSource> type = h2Properties.initializeDataSourceBuilder().type(HikariDataSource.class);
        return type.build();
    }

    @Bean(name = "secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hsqldb")
    public DataSource secondaryDataSource() {
        return hsqlProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    private Map<String, Object> getVendorProperties(DataSource dataSource) {
        Map<String, Object> properties = new HashMap<>();
        properties.put("hibernate.dialect", H2Dialect.class.getName());
        return properties;
    }
}
